
package com.example.zwx.myapplication;

import java.util.Arrays;
import java.util.StringTokenizer;

public class IrDataConversion {
    static final String TAG = "IrDataConversion";
    public static final int BLE_ONE_PACKET_DATA_USE_LEN = 16;
    public static final int BLE_ONE_PACKET_TOTAL_LEN = 20;
    public static final int IR_RAW_ONEKEY_DEF_LEN = 500;
    private byte[] mTempBuf = null;
    private byte[] mRawIrData = null;
    private byte curGroup = 0;
    private byte[] mLossyCompressionIrData = null;

    /**
     * constructor
     */
    public IrDataConversion() {
        this.mTempBuf = new byte[20];
        this.mRawIrData = new byte[500];
        this.mLossyCompressionIrData = new byte[500];
        Arrays.fill(this.mTempBuf, (byte) 0);
        Arrays.fill(this.mRawIrData, (byte) 0);
        Arrays.fill(this.mLossyCompressionIrData, (byte) 0);
    }

    protected boolean fillIrRawDataBuf(byte[] rawDataBuf) {
        this.mRawIrData = new byte[rawDataBuf.length];
        System.arraycopy(rawDataBuf, 0, this.mRawIrData, 0, rawDataBuf.length);
        return true;
    }

    protected boolean fillLossyCompressionIrDataBuf(byte[] lossyCompressionDataBuf) {
        this.mLossyCompressionIrData = new byte[lossyCompressionDataBuf.length];
        System.arraycopy(lossyCompressionDataBuf, 0, this.mLossyCompressionIrData, 0, lossyCompressionDataBuf.length);
        return true;
    }

    public byte charToByte(byte val) {
        byte tmp;
        if (val >= 48 && val <= 57) {
            tmp = (byte) (val - 48);
        } else if (val >= 97 && val <= 102) {
            tmp = (byte) (val - 87);
        } else if (val >= 65 && val <= 70) {
            tmp = (byte) (val - 55);
        } else {
            tmp = 0;
        }

        return tmp;
    }

    public byte StringToByte(String str) {
        if (str == null) {
            return (byte) 0;
        } else {
            byte[] buf = str.getBytes();
            byte val;
            if (buf.length >= 2) {
                val = this.charToByte(buf[0]);
                val = (byte) (val << 4);
                val += this.charToByte(buf[1]);
            } else if (buf.length == 1) {
                val = this.charToByte(buf[0]);
            } else {
                val = 0;
            }

            return val;
        }
    }

    public byte[] stringToByteBuf(String str) {
        byte[] valbuf = new byte[str.length()];
        StringTokenizer tokenizer = new StringTokenizer(str, " ");

        String tmpStr;
        int i;
        for (i = 0; tokenizer.hasMoreElements(); valbuf[i++] = this.StringToByte(tmpStr)) {
            tmpStr = tokenizer.nextToken();
        }

        byte[] outbuf = new byte[i];
        System.arraycopy(valbuf, 0, outbuf, 0, i);
        return outbuf;
    }

    private byte getBufCheckSum(byte[] buf, int len) {
        byte sum = 0;
        if (len > buf.length) {
            return (byte) 0;
        } else {
            for (int i = 0; i < len; ++i) {
                sum += buf[i];
            }

            return sum;
        }
    }

    protected byte[] getIrRawDataBufPacket(int index) {
        int tmpIrDatalen = this.mRawIrData.length;
        if (tmpIrDatalen < 6) {
            return null;
        } else if (this.mRawIrData[0] == 120 && this.mRawIrData[1] == 86) {
            if (this.mRawIrData[2] != 52 && this.mRawIrData[2] != 70) {
                return null;
            } else {
                Arrays.fill(this.mTempBuf, (byte) 0);
                this.mTempBuf[0] = 113;
                this.mTempBuf[1] = 0;
                if (index == 0) {
                    ++this.curGroup;
                    this.mTempBuf[1] = (byte) (this.mTempBuf[1] | 64);
                    int totalPackets = tmpIrDatalen / 16;
                    if (tmpIrDatalen % 16 > 0) {
                        ++totalPackets;
                    }

                    this.mTempBuf[2] = (byte) totalPackets;
                }

                this.mTempBuf[1] |= (byte) (this.curGroup & 1);
                int rdindex = index * 16;
                if (rdindex < tmpIrDatalen - 1) {
                    if (tmpIrDatalen - rdindex > 16) {
                        System.arraycopy(this.mRawIrData, rdindex, this.mTempBuf, 3, 16);
                        if (index != 0) {
                            this.mTempBuf[1] = (byte) (this.mTempBuf[1] & 15);
                            this.mTempBuf[1] = (byte) (this.mTempBuf[1] | 80);
                        }
                    } else {
                        System.arraycopy(this.mRawIrData, rdindex, this.mTempBuf, 3, tmpIrDatalen - rdindex);
                        if (index != 0) {
                            this.mTempBuf[1] = (byte) (this.mTempBuf[1] & 15);
                            this.mTempBuf[1] = (byte) (this.mTempBuf[1] | 96);
                        }
                    }

                    if (index != 0) {
                        this.mTempBuf[2] = (byte) (index + 1);
                    }

                    this.mTempBuf[19] = this.getBufCheckSum(this.mTempBuf, 19);
                    return this.mTempBuf;
                } else {
                    return null;
                }
            }
        } else {
            return null;
        }
    }

    public byte[] getIrLossyCompressionDataBufPacket(int index) {
        int tmpIrDatalen = this.mLossyCompressionIrData.length;
        if (tmpIrDatalen < 6) {
            return null;
        } else if (this.mLossyCompressionIrData[0] == 120 && this.mLossyCompressionIrData[1] == 86) {
            if (this.mLossyCompressionIrData[2] != 5 && this.mLossyCompressionIrData[2] != 7) {
                return null;
            } else {
                Arrays.fill(this.mTempBuf, (byte) 0);
                this.mTempBuf[0] = 113;
                this.mTempBuf[1] = 0;
                if (index == 0) {
                    ++this.curGroup;
                    this.mTempBuf[1] = (byte) (this.mTempBuf[1] | 16);
                    int totalPackets = tmpIrDatalen / 16;
                    if (tmpIrDatalen % 16 > 0) {
                        ++totalPackets;
                    }

                    this.mTempBuf[2] = (byte) totalPackets;
                }

                this.mTempBuf[1] |= (byte) (this.curGroup & 1);
                int rdindex = index * 16;
                if (rdindex < tmpIrDatalen - 1) {
                    if (tmpIrDatalen - rdindex > 16) {
                        System.arraycopy(this.mLossyCompressionIrData, rdindex, this.mTempBuf, 3, 16);
                        if (index != 0) {
                            this.mTempBuf[1] = (byte) (this.mTempBuf[1] & 15);
                            this.mTempBuf[1] = (byte) (this.mTempBuf[1] | 32);
                        }
                    } else {
                        System.arraycopy(this.mLossyCompressionIrData, rdindex, this.mTempBuf, 3, tmpIrDatalen - rdindex);
                        if (index != 0) {
                            this.mTempBuf[1] = (byte) (this.mTempBuf[1] & 15);
                            this.mTempBuf[1] = (byte) (this.mTempBuf[1] | 48);
                        }
                    }

                    if (index != 0) {
                        this.mTempBuf[2] = (byte) (index + 1);
                    }

                    this.mTempBuf[19] = this.getBufCheckSum(this.mTempBuf, 19);
                    return this.mTempBuf;
                } else {
                    return null;
                }
            }
        } else {
            return null;
        }
    }

    public int getIrRawArrayLength() {
        return mRawIrData.length;
    }

    public int getIrLossyArrayLength() {
        return mLossyCompressionIrData.length;
    }
}
